package com.yazi.mxz.person.controller;

import java.util.List;

import javax.annotation.Resource;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ListResult;
import com.yazi.mxz.common.entity.beans.Region;
import com.yazi.mxz.common.service.RegionService;
import com.yazi.mxz.person.jackson.filter.RegionFilter;

@Controller("regionController")
@RequestMapping("/region")
public class RegionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(RegionController.class);

	@Resource
	RegionService regionService;

	@RequestMapping("/listByParentId/json")
	@ResponseBody
	public ListResult<?> listByParentIdJson(Long parentId) {
		log.debug("获取子级地区数据");
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			Region region = new Region();
			region.setParentId(parentId);
			List<Region> regions = regionService.list(region);
			
			jsonResult.setResult(regions);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子级地区列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/listBrothers/json")
	@ResponseBody
	public ListResult<?> listBrothersJson(Long regionId) {
		log.debug("获取兄弟地区数据");
		ListResult<Region> jsonResult = new ListResult<Region>();
		jsonResult.setFilter(RegionFilter.get());
		try{
			Region region = regionService.one(regionId);
			if (region.getType() > 0) {
				Region region4s = new Region();
				region4s.setParentId(region.getParentId());
				List<Region> regions = regionService.list(region4s);
				jsonResult.setResult(regions);
			}
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取兄弟地区列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
}
